package com.example.applicationeventdemo.service;

import com.example.applicationeventdemo.pojo.UserArriveEvent;
import com.example.applicationeventdemo.pojo.UserRegisteredEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private final ApplicationEventPublisher eventPublisher;

    // 构造函数注入
    public UserService(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public void registerUser(String username) {
        System.out.println(Thread.currentThread().getName() + "线程:用户注册成功: " + username);
        // 发布事件
        eventPublisher.publishEvent(new UserRegisteredEvent(username));
    }

    public void userArrive(String username) {
        System.out.println(Thread.currentThread().getName() + "线程:用户到达: " + username);
        // 发布事件
        eventPublisher.publishEvent(new UserArriveEvent(username));
    }
}